package com.winning.devops.batch.split;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

/**
 * 并发执行Job
 *
 * @author chensj
 * @date 2020-02-28 15:38
 */
@Configuration
@EnableBatchProcessing
public class JobSplitDemo {


    /**
     * 创建Job的bean
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * job 由step组成
     * 创建Step 对象的bean
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job splitDemoJob () {
        return jobBuilderFactory
                .get("splitDemoJob")
                // 指明第一个flow
                .start(splitFlow1())
                // split 指定 Executor 用于并发
                .split(new SimpleAsyncTaskExecutor())
                // 添加并发的flow
                .add(splitFlow2())
                .end()
                .build();

    }

    @Bean
    public Flow splitFlow1 () {
        return new FlowBuilder<Flow>("splitFlow1")
                .start(splitJobDemoStep1())
                .next(splitJobDemoStep2())
                .next(splitJobDemoStep3())
                .build();
    }

    @Bean
    public Flow splitFlow2 () {
        return new FlowBuilder<Flow>("splitFlow2")
                .start(splitJobDemoStep4())
                .next(splitJobDemoStep5())
                .next(splitJobDemoStep6())
                .build();
    }


    @Bean
    public Step splitJobDemoStep1 () {
        return stepBuilderFactory
                .get("splitJobDemoStep1")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("splitJobDemoStep1");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step splitJobDemoStep2 () {
        return stepBuilderFactory
                .get("splitJobDemoStep2")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("splitJobDemoStep2");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step splitJobDemoStep3 () {
        return stepBuilderFactory
                .get("splitJobDemoStep3")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("splitJobDemoStep3");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step splitJobDemoStep4 () {
        return stepBuilderFactory
                .get("splitJobDemoStep4")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("splitJobDemoStep4");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step splitJobDemoStep5 () {
        return stepBuilderFactory
                .get("splitJobDemoStep5")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("splitJobDemoStep5");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step splitJobDemoStep6 () {
        return stepBuilderFactory
                .get("splitJobDemoStep6")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("splitJobDemoStep6");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }
}
